
#include "stdafx.h"
#include "ReloadCode.h"
#include "ReloadPacket.h"
#include "ReloadConfigure.h"
#include "ReloadUdpCallPacket.h"

#define INFO_PROTOCOL_RELOAD_FIXEDHEADER_NOT_BEGIN_WITH_RELOAD_TOKEN				10049

SerializeString::SerializeString()
{

}

SerializeString::SerializeString( const std::vector<byte>& _array )
{
	R0(setValue(_array));
}

SerializeString::SerializeString( const char* _cstr )
{
	R0(setValue(_cstr));
}

SerializeString::SerializeString( const std::string& _str )
{
	R0(setValue(_str));
}

SerializeString::~SerializeString()
{

}

retT SerializeString::setValue( const std::vector<byte>& _array )
{
	array_.clear();
	array_.resize(_array.size());
	std::copy(_array.begin(), _array.end(), array_.begin());
	RETSUCCESS;
}

retT SerializeString::setValue( const char* _cstr )
{
	array_.clear();
	array_.resize(strlen(_cstr));
	for(std::size_t i = 0; i < array_.size(); ++i)
		array_[i] = (byte)_cstr[i];
	RETSUCCESS;
}

retT SerializeString::setValue( const std::string& _str )
{
	array_.clear();
	array_.resize(_str.length());
	for(std::size_t i = 0; i < array_.size(); ++i)
		array_[i] = (byte)_str[i];
	RETSUCCESS;
}

std::string SerializeString::toString()
{
	if(array_.size() > 0)
		return std::string((char*)&array_[0], array_.size());
	return std::string();
}

/////////////////////////////////////////////////////////////////////////////////////////////

ReloadUdpCallPacketBase::ReloadUdpCallPacketBase()
{

}

ReloadUdpCallPacketBase::~ReloadUdpCallPacketBase()
{

}

retT ReloadUdpCallPacketBase::readFromBufferNoCheck( boost::shared_ptr<IoBuffer> _pBuffer )
{
	uint32 reloToken;
	R(_pBuffer->ntohRead(reloToken));
	if(reloToken != RELOAD_TOKEN)
	{
		RET(INFO_PROTOCOL_RELOAD_FIXEDHEADER_NOT_BEGIN_WITH_RELOAD_TOKEN);
	}
	R(overlayInstanceName_.readFromBuffer(_pBuffer));
	R(resourceName_.readFromBuffer(_pBuffer));
	RETSUCCESS;
}

retT ReloadUdpCallPacketBase::writeToBufferNoCheck( boost::shared_ptr<IoBuffer> _pBuffer )
{
	uint32 reloToken = RELOAD_TOKEN;
	R(_pBuffer->htonWrite(reloToken));
	R(overlayInstanceName_.writeToBuffer(_pBuffer));
	R(resourceName_.writeToBuffer(_pBuffer));
	RETSUCCESS;
}

retT ReloadUdpCallPacketBase::readFromBuffer( boost::shared_ptr<IoBuffer> _pBuffer )
{
	READFROMBUFFER_START;
	R(readFromBufferNoCheck(_pBuffer));
	READFROMBUFFER_END;
}

retT ReloadUdpCallPacketBase::writeToBuffer( boost::shared_ptr<IoBuffer> _pBuffer )
{
	WRITETOBUFFER_START;
	R(writeToBufferNoCheck(_pBuffer));
	WRITETOBUFFER_END;
}

std::size_t ReloadUdpCallPacketBase::size()
{
	return sizeof(uint32) + overlayInstanceName_.size() + resourceName_.size();
}

retT ReloadUdpCallPacketBase::attachRootElement( boost::weak_ptr<PacketElement> _pRootElement )
{
	R(PacketElement::attachRootElement(_pRootElement));
	R(overlayInstanceName_.attachRootElement(_pRootElement));
	R(resourceName_.attachRootElement(_pRootElement));
	RETSUCCESS;
}

/////////////////////////////////////////////////////////////////////////////////////////////

SendStoreReqUdpCallPacket::SendStoreReqUdpCallPacket()
{

}

SendStoreReqUdpCallPacket::~SendStoreReqUdpCallPacket()
{

}

retT SendStoreReqUdpCallPacket::readFromBuffer( boost::shared_ptr<IoBuffer> _pBuffer )
{
	READFROMBUFFER_START;
	R(readFromBufferNoCheck(_pBuffer));
	R(kindData_.readFromBuffer(_pBuffer));
	READFROMBUFFER_END;
}

retT SendStoreReqUdpCallPacket::writeToBuffer( boost::shared_ptr<IoBuffer> _pBuffer )
{
	WRITETOBUFFER_START;
	R(writeToBufferNoCheck(_pBuffer));
	R(kindData_.writeToBuffer(_pBuffer));
	WRITETOBUFFER_END;
}

std::size_t SendStoreReqUdpCallPacket::size()
{
	return ReloadUdpCallPacketBase::size() + kindData_.size();
}

/////////////////////////////////////////////////////////////////////////////////////////////

SendFetchReqUdpCallPacket::SendFetchReqUdpCallPacket()
{

}

SendFetchReqUdpCallPacket::~SendFetchReqUdpCallPacket()
{

}

retT SendFetchReqUdpCallPacket::readFromBuffer( boost::shared_ptr<IoBuffer> _pBuffer )
{
	READFROMBUFFER_START;
	R(readFromBufferNoCheck(_pBuffer));
	R(specifiers_.readFromBuffer(_pBuffer));
	READFROMBUFFER_END;
}

retT SendFetchReqUdpCallPacket::writeToBuffer( boost::shared_ptr<IoBuffer> _pBuffer )
{
	WRITETOBUFFER_START;
	R(writeToBufferNoCheck(_pBuffer));
	R(specifiers_.writeToBuffer(_pBuffer));
	WRITETOBUFFER_END;
}

std::size_t SendFetchReqUdpCallPacket::size()
{
	return ReloadUdpCallPacketBase::size() + specifiers_.size();
}

retT SendFetchReqUdpCallPacket::attachRootElement( boost::weak_ptr<PacketElement> _pRootElement )
{
	R(ReloadUdpCallPacketBase::attachRootElement(_pRootElement));
	R(specifiers_.attachRootElement(_pRootElement));
	RETSUCCESS;
}

/////////////////////////////////////////////////////////////////////////////////////////////

ReloadUdpCallResultBase::ReloadUdpCallResultBase()
	:result_(0)
{

}

ReloadUdpCallResultBase::~ReloadUdpCallResultBase()
{
	result_ = 0;
}

retT ReloadUdpCallResultBase::readFromBufferNoCheck( boost::shared_ptr<IoBuffer> _pBuffer )
{
	uint32 reloToken;
	R(_pBuffer->ntohRead(reloToken));
	if(reloToken != RELOAD_TOKEN)
	{
		RET(INFO_PROTOCOL_RELOAD_FIXEDHEADER_NOT_BEGIN_WITH_RELOAD_TOKEN);
	}
	R(_pBuffer->ntohRead(result_));
	RETSUCCESS;
}

retT ReloadUdpCallResultBase::writeToBufferNoCheck( boost::shared_ptr<IoBuffer> _pBuffer )
{
	uint32 reloToken = RELOAD_TOKEN;
	R(_pBuffer->htonWrite(reloToken));
	R(_pBuffer->htonWrite(result_));
	RETSUCCESS;
}

retT ReloadUdpCallResultBase::readFromBuffer( boost::shared_ptr<IoBuffer> _pBuffer )
{
	READFROMBUFFER_START;
	R(readFromBufferNoCheck(_pBuffer));
	READFROMBUFFER_END;
}

retT ReloadUdpCallResultBase::writeToBuffer( boost::shared_ptr<IoBuffer> _pBuffer )
{
	WRITETOBUFFER_START;
	R(writeToBufferNoCheck(_pBuffer));
	WRITETOBUFFER_END;
}

std::size_t ReloadUdpCallResultBase::size()
{
	return sizeof(uint32) + sizeof(result_);
}

/////////////////////////////////////////////////////////////////////////////////////////////

SendFetchReqUdpCallResult::SendFetchReqUdpCallResult()
{

}

SendFetchReqUdpCallResult::~SendFetchReqUdpCallResult()
{

}

retT SendFetchReqUdpCallResult::readFromBuffer( boost::shared_ptr<IoBuffer> _pBuffer )
{
	READFROMBUFFER_START;
	R(readFromBufferNoCheck(_pBuffer));
	R(kindResponses_.readFromBuffer(_pBuffer));
	READFROMBUFFER_END;
}

retT SendFetchReqUdpCallResult::writeToBuffer( boost::shared_ptr<IoBuffer> _pBuffer )
{
	WRITETOBUFFER_START;
	R(writeToBufferNoCheck(_pBuffer));
	R(kindResponses_.writeToBuffer(_pBuffer));
	WRITETOBUFFER_END;
}

std::size_t SendFetchReqUdpCallResult::size()
{
	return ReloadUdpCallResultBase::size() + kindResponses_.size();
}

retT SendFetchReqUdpCallResult::attachRootElement( boost::weak_ptr<PacketElement> _pRootElement )
{
	R(PacketElement::attachRootElement(_pRootElement));
	R(kindResponses_.attachRootElement(_pRootElement));
	RETSUCCESS;
}

/////////////////////////////////////////////////////////////////////////////////////////////

SendAppAttachReqUdpCallResult::SendAppAttachReqUdpCallResult()
{

}

SendAppAttachReqUdpCallResult::~SendAppAttachReqUdpCallResult()
{

}

retT SendAppAttachReqUdpCallResult::readFromBuffer( boost::shared_ptr<IoBuffer> _pBuffer )
{
	READFROMBUFFER_START;
	R(readFromBufferNoCheck(_pBuffer));
	R(strIP_.readFromBuffer(_pBuffer));
	READFROMBUFFER_END;
}

retT SendAppAttachReqUdpCallResult::writeToBuffer( boost::shared_ptr<IoBuffer> _pBuffer )
{
	WRITETOBUFFER_START;
	R(writeToBufferNoCheck(_pBuffer));
	R(strIP_.writeToBuffer(_pBuffer));
	WRITETOBUFFER_END;
}

std::size_t SendAppAttachReqUdpCallResult::size()
{
	return ReloadUdpCallResultBase::size() + strIP_.size();
}

retT SendAppAttachReqUdpCallResult::attachRootElement( boost::weak_ptr<PacketElement> _pRootElement )
{
	R(PacketElement::attachRootElement(_pRootElement));
	R(strIP_.attachRootElement(_pRootElement));
	RETSUCCESS;
}
